﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace SMP.Client.PolForm
{
    using System.Collections;
    using SMP.Windows;
    using SMP.Service;
    using SMP.Domain;

    public partial class FrmVaitro : SMP.Windows.Forms.FrmBase
    {
        Vaitro_Service _objVaitro = new Vaitro_Service();
        DataTable dtb_vaitro = new DataTable();
        DataTable dtb_form = new DataTable();
        Vaitro vaitro = new Vaitro();
        DataSet dsForm = new DataSet();
        object identity;

        public FrmVaitro()
        {
            InitializeComponent();
            item_Print.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            item_Select.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            item_Report.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            item_Add.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            item_Delete.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
        }

        #region DisplayInfo, ResetText, ChangeStatus, ClearDataBindings, DataBindingControl

        public override void DisplayInfo()
        {
            try
            {                
                this.FormState = Windows.Forms.FormState.View;
                var dtb_thanhvien = _objVaitro.GetAll_Thanhvien();
                dgThanhvien.DataSource = dtb_thanhvien;

                dtb_form = _objVaitro.GetAll_Form();
                if (!dtb_form.Columns.Contains("Chon"))
                    dtb_form.Columns.Add("Chon", typeof(bool));
                
                ChangeStatus();
            }
            catch (Exception ex)
            {
                Windows.Forms.Message.Show(ex.Message, ex.ToString());
            }
        }

        public override void ChangeStatus(bool editTable = true)
        {
            btnChon.Enabled = !editTable;
            btnXoa.Enabled = !editTable;
            gvForm.OptionsBehavior.Editable = !editTable;
        }
        #endregion


        #region PerformAdd, PerformEdit, PerformDelete, PerformCancel, PerformSave, PerformSelectOneObject
        
        public override bool PerformEdit()
        {
            ChangeStatus(false);
            return true;
        }

        public override bool PerformCancel()
        {
            DisplayInfo();
            return true;
        }

        public override bool PerformSave()
        {
            var save = false;

            identity = gvThanhvien.GetFocusedRowCellValue("Id_Thanhvien");
            save = _objVaitro.Update_Collection_Vaitro(dtb_vaitro, identity);

            if (save)
                DisplayInfo();
            gvThanhvien.FocusedRowHandle = gvThanhvien.LocateByValue(0, gvThanhvien.Columns["Id_Thanhvien"], identity);
            return save;
        }

        #endregion


        #region DisplayText

        private void gvThanhvien_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                if (e.Column.FieldName == "STT")
                    e.DisplayText = (Convert.ToInt64(e.RowHandle) + 1).ToString();
            }
        }

        private void gvForm_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                if (e.Column.FieldName == "STT")
                    e.DisplayText = (Convert.ToInt64(e.RowHandle) + 1).ToString();
            }
        }

        private void gvVaitro_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                if (e.Column.FieldName == "STT")
                    e.DisplayText = (Convert.ToInt64(e.RowHandle) + 1).ToString();
            }
        }

        #endregion


        #region Method

        private void btnChon_Click(object sender, EventArgs e)
        {
            if (this.FormState != Windows.Forms.FormState.View)
            {
                if (gvThanhvien.GetFocusedRowCellValue("Id_Thanhvien") + "" == "")
                {
                    Windows.Forms.Message.Show("Chưa chọn Thành viên.\nVui lòng chọn Thành viên", "Thông Báo", MessageBoxButtons.OK);
                    return;
                }

                DataRow[] SelectRow = dtb_form.Select("Chon = 'True'");
                if (SelectRow.Length > 0)
                {
                    dtb_vaitro.Clear();
                    dtb_vaitro = dtb_form.Copy().Clone();
                    foreach (DataRow dr in SelectRow)
                    {
                        var newRow = dtb_vaitro.NewRow();
                        foreach (DataColumn col in dtb_vaitro.Columns)
                        {
                            try { newRow[col.ColumnName] = dr[col.ColumnName]; }
                            catch { continue; }
                        }
                        dtb_vaitro.Rows.Add(newRow);
                    }
                    dgVaitro.DataSource = dtb_vaitro;
                }
                else
                    dgVaitro.DataSource = null;
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (gvVaitro.GetFocusedRowCellValue("Id_Form") + "" == "")
                return;
            DataRow dr = gvVaitro.GetDataRow(gvVaitro.FocusedRowHandle);
            dtb_vaitro.Rows.Remove(dr);

        }

        private void gridChon_Form_CheckedChanged(object sender, EventArgs e)
        {
            DataRow[] row = dtb_form.Select(string.Format("Id_Form = {0}", gvForm.GetFocusedRowCellValue("Id_Form")));
            row[0]["Chon"] = gridChon_Form.Editable;
        }

        private void CheckAll_Grid(bool check = true)
        {
            foreach (DataRow r in dtb_form.Rows)            
                r["Chon"] = check;            
        }

        private void gvThanhvien_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gvThanhvien.GetFocusedRowCellValue("Id_Thanhvien") + "" != "")
            {
                if (this.FormState != Windows.Forms.FormState.View)
                {
                    var guid_ctu = gvThanhvien.GetFocusedRowCellValue("Guid_Ctu") + "";
                    if (!dsForm.Tables.Contains(guid_ctu))
                    {
                        dtb_form.TableName = guid_ctu;
                        dsForm.Tables.Add(dtb_form.Copy());
                    }
                    dgForm.DataSource = dsForm.Tables[guid_ctu];
                }
                else
                {
                    dgForm.DataSource = dtb_form;
                    dtb_vaitro = _objVaitro.GetAll_Form_By_Id_Thanhvien(gvThanhvien.GetFocusedRowCellValue("Id_Thanhvien"));
                    dgVaitro.DataSource = dtb_vaitro;
                }
            }
            else
            {
                dgForm.DataSource = null;
                dgVaitro.DataSource = null;
            }

        }


        #endregion








    }
}